﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
namespace Invoicing.AJAX
{
    /// <summary>
    /// UploadFile 的摘要说明
    /// </summary>
    public class UploadFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain"; 
            context.Response.Charset = "utf-8";
            HttpPostedFile file = context.Request.Files[0];
            int filecount = context.Request.Files.Count;
            string uploadPath = HttpContext.Current.Server.MapPath(@"\\TxFile") + "\\"; 

            if (file != null)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                file.SaveAs(uploadPath + file.FileName);
                //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                context.Response.Write(Pubs.AjaxHandle.AjaxPress("200",file.FileName));
            }
            else
            {
                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", file.FileName));
            }  



        }



        /// <summary>
        /// 检查是否为合法的上传文件
        /// </summary>
        /// <param name="_fileExt"></param>
        /// <returns></returns>
        private bool CheckFileExt(string _fileExt)
        {
            string[] allowExt = new string[] { ".gif", ".jpg", ".png" };
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i] == _fileExt) { return true; }
            }
            return false;

        }

        /// <summary>
        /// 获取文件名
        /// </summary>
        /// <param name="fileNamePath"></param>
        /// <returns></returns>
        private string getName(string fileNamePath)
        {
            string[] name = fileNamePath.Split('\\');
            return name[name.Length - 1];
        }
        public string UpLoadFile(HttpPostedFile httpFile, string toFilePath)
        {
            try
            {
                //获取要保存的文件信息
                string filerealname = httpFile.FileName;
                //获得文件扩展名
                string fileNameExt = System.IO.Path.GetExtension(filerealname);
                if (CheckFileExt(fileNameExt))
                {
                    //检查保存的路径 是否有/结尾
                    if (toFilePath.EndsWith("/") == false) toFilePath = toFilePath + "/";
                    //物理完整路径                    
                    string toFileFullPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + toFilePath;

                    //检查是否有该路径  没有就创建
                    if (!System.IO.Directory.Exists(toFileFullPath))
                    {
                        Directory.CreateDirectory(toFileFullPath);
                    }

                    //得到服务器文件保存路径
                    string toFile = HttpContext.Current.Server.MapPath("~" + toFilePath);
                    string f_file = getName(filerealname);
                    //将文件保存至服务器

                    // 检查文件是否存在

                    FileInfo info = new FileInfo(toFile + f_file);
                    string temp = DateTime.Now.Millisecond.ToString();
                    if (info.Exists)
                    {
                        httpFile.SaveAs(toFile + temp + f_file);
                        filerealname = temp + filerealname;
                    }
                    else
                    {
                        httpFile.SaveAs(toFile + f_file);
                        filerealname = f_file;
                    }
                    return   Pubs.AjaxHandle.Success("200", filerealname);
                }
                else
                {
                    return Pubs.AjaxHandle.AjaxPress("300", "文件类型不合法");
                }
            }
            catch (Exception e)
            {
                return Pubs.AjaxHandle.AjaxPress("300", e.Message);
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}